﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SurfaceAd.Common;
using SurfaceAd.Entities;
using Windows.Devices.Geolocation;
using Windows.Foundation;

/**
 * 创建时间：2014/9/24 11:17:50
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace SurfaceAd.Assistant
{
    internal sealed class GeolocatorHelper
    {
        private static Geolocator geolocator = null;

        static GeolocatorHelper()
        {

        }

        public static IAsyncOperation<GeolocatorInfo> GetGeolocatorLocationAsyncOperation()
        {
            return GetGeolocatorLocationAsync().AsAsyncOperation();
        }

        /// <summary>
        /// 
        /// </summary>
        private static async Task<GeolocatorInfo> GetGeolocatorLocationAsync()
        {
            GeolocatorInfo geolocatorInfo = new GeolocatorInfo()
            {
                Latitude = -1,
                Longitude = -1,
                Altitude = -1,
                Accuracy = -1
            };
            try
            {
                if (geolocator == null)
                {
                    geolocator = new Geolocator();
                }
                Geoposition position = await geolocator.GetGeopositionAsync();
                if (position != null)
                {
                    geolocatorInfo.Accuracy = position.Coordinate.Accuracy;
                    geolocatorInfo.Altitude = position.Coordinate.Point.Position.Altitude;
                    geolocatorInfo.Longitude = position.Coordinate.Point.Position.Longitude;
                    geolocatorInfo.Latitude = position.Coordinate.Point.Position.Latitude;
                }
            }
            catch (Exception err)
            {
                LogHelper.LogError(err.ToString(), typeof(GeolocatorHelper), "GeolocatorHelper");                
            }
            return geolocatorInfo;
        }
    }
}
